<?php
#include_once "BaseController.php";

class IndexController extends BaseController 
{

    public function init()
    {
        
        /* Initialize action controller here */
    }
    public function preDispatch()
	{
	   parent::preDispatch();
    }
    public function indexAction()
    {
                        
            // Query for the visitors IP address
        $mMotorSearch = new MotorSearch();
        $page = $this->_request->getParam('page',1);
    	$page = $page ? intval($page):1;
    	$rows_per_page = 10; 
    	$offset = ($page-1) * $rows_per_page;
        $arrMotors = $mMotorSearch->getMotors(' AND m.active=1 AND m.type='.$this->type, ' ORDER BY created_time DESC', $offset, $rows_per_page);
        $html ='';
        $sClass='';
        $i=0;
        $iCount=count($arrMotors['result']);
        foreach ($arrMotors['result'] as $row){
            if($row['image'] && file_exists(UPLOAD.'default/item/'.$row['image']) ){
            	 $this->view->image = UPLOAD_DIR.'default/item/'.$row['image'];
             //   $this->view->image = UPLOAD_DIR.$row['image'];
            }else{
                $this->view->image = IMAGE_URL.'no_img.png';
            }
			
			if ($row['manu_name']=="Khác" && $row['manu_khac']!="Thương hiệu xe của bạn là gì?") $row['manu_name'] = $row['manu_khac'];
			if ($row['model_name']=="Khác" && $row['model_khac']!="Model xe của bạn là gì? ") $row['model_name']=$row['model_khac'];
			$this->view->name = $row['manu_name'] . ' '.$row['model_name'];
			if ($this->view->name=="Khác Khác"&&($this->type==0)) $this->view->name = "Xe máy cũ";
			else if ($this->view->name=="Khác Khác"&&($this->type==1)) $this->view->name = "Ô tô cũ";
            $this->view->date = date('d/m/Y', strtotime($row['created_time']));
            
            if ($row['price']){
                $this->view->price = number_format($row['price'],0,'','.') .' VNĐ';//$row['price'] . ' Triệu';
            }else{
                $this->view->price = 'Thỏa thuận';//$row['price_from'] .' Triệu - '. $row['price_to'].' Triệu';
            }
            $this->view->city = $row['city_name'];
            $this->view->brand = $row['manu_name'];
            if(($iCount%2==0 && ($iCount-$i)<=2) || ($iCount%2!=0 && ($iCount-$i)<=1)){
                $sClass='listLast';
            }
            $i++;
            $this->view->SCLASS=$sClass;
            
            $link = $this->view->translate('by_type_detail_'.$this->type);
            
            $link1 = Common::urlParser(array ($row['alias']), $link).'.html';
            $link1 = str_replace ('([0-9]+)',$row['id'],$link1);
            
            $this->view->link='/'.$link1;
            $html .= $this->view->render('search/item.phtml');
        }
        
        
        $this->view->LIST = $html;
        
//        $pager = LoadObject::startPaging();
//    	$sPaging_html = $pager->pager($arrMotors['total'], $rows_per_page, $page, 'index/paging.phtml');
    	
        $pager = new Defined_PageBreak();
        $maxpage = ceil ($arrMotors['total']/$rows_per_page);
        $sPaging_html = $pager->wp_page_numbers($maxpage, $page);
            
    	$this->view->PAGINATOR = $sPaging_html;
        
        if(!isset ($_SESSION['popup']) || $_SESSION['popup']==''){
            
            $this->view->popup .= '<script type="text/javascript" src="'.JS_URL.'jquery.blockUI.js"></script>'."\n";
            $this->view->popup .= "<script type='text/javascript'>
            jQuery(function(){
                jQuery.blockUI({ 
                    message: jQuery('#divPopup'),
                    css:{
                        'top':'20%',
                        'left':'20%',
                        'width':'657px',
                        //'height':'220px',
                        'line-height':'40px',
                        'font-weight':'bold',
                        'cursor': 'default'
                    },
                    overlayCSS:{
                        'cursor': 'default'
                    }    
                });
            });
            </script>\n";
            
            $_SESSION['popup'] = 'popup';
        }
        //echo $this->view->render('index/index.phtml');
    }
    function logoutAction() {
    	
    	$authenticate=new defined_Authenticate();
    	$authenticate->logout();
    	
		$this->_redirect('/');
		exit();
    }  
    
}

